package com.xfcode.ai.sdk;

import com.xfcode.ai.platform.MoonClient;
import com.xfcode.ai.platform.bean.ChatCompletionMessage;
import com.xfcode.ai.platform.bean.ChatCompletionRequest;
import com.xfcode.ai.platform.bean.ChatCompletionStreamResponse;
import com.xfcode.ai.platform.bean.ChatMessageRole;
import io.reactivex.Flowable;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Getter
public class ChatModelClient {
    private String appKey;
    private GptType gptType;

    private MoonClient moonClient;
    public ChatModelClient(String appKey) {
        if (appKey == null){
            throw new IllegalStateException("appKey is null");
        }
        this.appKey = appKey;
        this.gptType = GptType.KIMI;
        this.moonClient = new MoonClient(appKey);
    }
    public ChatModelClient(String appKey, GptType gptType) {
        this.appKey = appKey;
        this.gptType = gptType;
        this.moonClient = new MoonClient(appKey);
    }
    public String call(ChatCompletionMessage... inputs) {
        List<ChatCompletionMessage> messages= Arrays.asList(inputs);
        return  moonClient.chatStreamCompletion(getChatCompletionRequest(messages, 2000));
    }

    public String call( List<ChatCompletionMessage> messages) {
        return  moonClient.chatStreamCompletion(getChatCompletionRequest(messages, 2000));
    }

    public String call( String input) {
        ChatCompletionMessage message = new ChatCompletionMessage(ChatMessageRole.USER.value(), input);
        return this.call(message);
    }



    public String call(String template, String input) {
        List<ChatCompletionMessage> messages=new ArrayList<>();
        messages.add(new ChatCompletionMessage(ChatMessageRole.SYSTEM.value(),template));
        messages.add(new ChatCompletionMessage(ChatMessageRole.USER.value(), input));
        ChatCompletionRequest chatCompletionRequest = getChatCompletionRequest(messages, 2000);
        try {
          return getMoonClient().chatCompletion(chatCompletionRequest).getChoices().get(0).getMessage().getContent();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public Flowable<ChatCompletionStreamResponse> stream(String message) {
        List<ChatCompletionMessage> messages = new ArrayList<>();
        messages.add(new ChatCompletionMessage(ChatMessageRole.USER.value(), message));
        ChatCompletionRequest chatCompletionRequest =getChatCompletionRequest(messages, 2000);
        try {
            return  getMoonClient().chatCompletionStream(chatCompletionRequest);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public Flowable<ChatCompletionStreamResponse> stream(List<ChatCompletionMessage> messages) {
        try {
            return  moonClient.chatCompletionStream(getChatCompletionRequest(messages, 2000));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    private static @NotNull ChatCompletionRequest getChatCompletionRequest(List<ChatCompletionMessage> messages, int maxTokens) {
        return new ChatCompletionRequest(
//                "moonshot-v1-128k-vision-preview",
                "moonshot-v1-128k",
                messages,
                maxTokens,
                0.3f,
                1
        );
    }
}
